/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.phonebook;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.phonebook.Phonebook;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-01-31
 */
@Service("phonebookService")
public class PhonebookServiceImpl implements PhonebookService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Phonebook
     ****************************************/
    public void insert(Phonebook phonebook) throws Exception {
        phonebook.setPhoneid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(phonebook);
    }

    /****************************************
     * Update a instace of Phonebook
     ****************************************/
    public void update(Phonebook phonebook) throws Exception {
        iDataSet4iBatis.update(phonebook);
    }

    /****************************************
     * Delete a instace of Phonebook
     ****************************************/
    public void delete(Phonebook phonebook) throws Exception {
        iDataSet4iBatis.delete(phonebook);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Phonebook phonebook) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Phonebook phonebook) throws Exception {
        String statementId = phonebook.getStatmentId();
        String mainId = phonebook.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> phonebookList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Phonebook model = new Phonebook();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            phonebookList.add(model);
        }
        iDataSet4iBatis.batchDelete(phonebookList);
    }

    /****************************************
     * Query instaces of Phonebook
     ****************************************/
    public List<BaseModel> queryAsList(Phonebook phonebook) throws Exception {
        String queryListStatement = "";
        if (phonebook.isPaging()) {
            queryListStatement = phonebook.getStatmentId();
            phonebook.setStatmentId("Phonebook.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(phonebook);
            phonebook.setTotalCount(baseModel.getTotalCount());
            phonebook.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(phonebook);
    }

    /****************************************
     * Query a instaces of Phonebook
     ****************************************/
    public BaseModel queryAsObject(Phonebook phonebook) throws Exception {
        return iDataSet4iBatis.queryAsObject(phonebook);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
